<?php
/**
 * Yotaku Shop - yshop
 * @author Creatint<creatint@163.com>
 * @copyright www.yotaku.cn 2016
 */
namespace Home\Model;
use Think\Model;
use Think\Upload;
class AddressModel extends Model{
    public $error   =   '';
    //自动完成
    protected $_auto = array(
        array('create_time', NOW_TIME, self::MODEL_INSERT),
        array('update_time', NOW_TIME, self::MODEL_BOTH),
    );

    public function update($data){
        if(!$this->create($data)){
            $this->error = '地址数据错误';
            return false;
        }
        if($data['id']>0){
            if(false === $this->save()){
                $this->error = '修改地址错误';
                return false;
            }
        }else{
            if(!($id = $this->add())){
                $this->error = '新增地址错误';
                return false;
            }
            $data['id'] = $id;
        }
        return $data;
    }
    public function getAddress($user_id){
        $map = array();
        $map['user_id'] = $user_id;
        $res = $this->where($map)->select();
        return $res;
    }
    public function getAddressById($id){
        $res = $this->find($id);
        return $res;
    }
    public function getOne($user_id){
        $map['user_id'] = $user_id;
        $res = $this->where($map)->order("`default` DESC, `status` DESC, `update_time` DESC")->limit(1)->find();
        return $res;
    }
    public function del($id){
        if(empty($id)){
            $this->error = '请选择要删除的配置项';
        }
        $map = array('id'=>array('IN',trim(implode(',',$id),',')));
        //$res = $this->where($map)->delete();
        if($this->where($map)->delete()){
            return true;
        }else{
            $this->error = '无此条数据';
            return false;
        }
    }
}